import { Controller, Inject, Get, Query, Body, Post, Put, Param, Del } from '@midwayjs/core';
import { JobService } from "@service/monitor/job.service";
import { ListJobDTO, CreateJobDTO, UpdateJobDTO, ChangeStatusDto, RunJobDto } from "@dto/monitor/job.dto";
import { Auth } from "@decorator/auth.decorator";
import { BusinessType, Log } from "@decorator/log.decorator";

@Controller('/monitor/job')
export class PostController {
  @Inject()
  jobService: JobService;

  // 获取列表
  @Auth('monitor:job:list')
  @Get('/list')
  async list(@Query() queryParams: ListJobDTO) {
    return await this.jobService.list(queryParams);
  }

  // 新增
  @Auth('monitor:job:add')
  @Log({ title: '定时任务', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() job: CreateJobDTO) {
    return await this.jobService.create(job);
  }

  // 删除
  @Auth('monitor:job:remove')
  @Log({ title: '定时任务', businessType: BusinessType.REMOVE })
  @Del('/:jobId')
  async delete(@Param('jobId') jobId: string) {
    return await this.jobService.delete(jobId);
  }

  // 修改
  @Auth('monitor:job:edit')
  @Log({ title: '定时任务', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() job: UpdateJobDTO) {
    return await this.jobService.update(job);
  }

  // 获取详情
  @Auth('monitor:job:query')
  @Get('/:jobId')
  async get(@Param('jobId') jobId: number) {
    return await this.jobService.detail(jobId);
  }

  // 导出
  @Auth('monitor:job:export')
  @Log({ title: '定时任务', businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Query() queryParams: ListJobDTO) {
    return await this.jobService.export(queryParams);
  }

  // 改变状态
  @Auth('monitor:job:changeStatus')
  @Put('/changeStatus')
  async changeStatus(@Body() body: ChangeStatusDto) {
    return await this.jobService.changeStatus(body);
  }

  // 执行一次
  @Auth('monitor:job:run')
  @Put('/run')
  async run(@Body() body: RunJobDto) {
    return await this.jobService.run(body);
  }
}
